﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NENAData.Errors;
using NENAData.Transactions.Payments.PayPal;
using NENAData.Payments;
using NENAData.Email;

namespace PayPal
{
    public partial class EventConfirmation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                NENAEventConfirmation();
            }
            catch (Exception ex)
            {
                ErrHandler.WriteError("In OrderConfirmation: Form_Load: <br/>" + ex.Message);
                Response.Write("Sorry, an Error Occurred <br />" + ex.InnerException + "<br />" + ex.Message + "<br />");
            }
        }

        protected void NENAEventConfirmation()
        {
            try
            {

                //NVPAPICaller NENANVP = new NVPAPICaller();

                //string retMsg = "";
                //string strtoken = "";
                //string finalPaymentAmount = "";
                //string payerId = "";
                //int intAffiliateID = Convert.ToInt32(Session["Affiliate_ID"].ToString());
                ////NVPCodec decoder;
                //var decoder = new NVPCodec();
                
                //strtoken = Request.QueryString["token"] == "" ? Session["token"].ToString() : Request.QueryString["token"];
                //payerId = Request.QueryString["PayerId"] == "" ? Session["PayerId"].ToString() : Request.QueryString["PayerId"];

                //finalPaymentAmount = Session["payment_amt"].ToString();
                //Response.Write("payment_amt: " + finalPaymentAmount);
                //Response.Write("<br />payerId: " + payerId);
                //Response.Write("<br />token: " + strtoken);

                ////todo needs to be updated to the newest API
                //bool boolSuccess = NENANVP.ConfirmPayment(finalPaymentAmount, "Event Fees", strtoken, payerId, ref decoder, ref retMsg, intAffiliateID);
                //Response.Write("<br />Amount: " + decoder["AMT"]);
                //Response.Write("<br />Currency: " + decoder["CURRENCYCODE"]);
                //Response.Write("<br />transactionId: " + decoder["TRANSACTIONID"]);
                //Response.Write("<br />TRANSACTIONTYPE: " + decoder["TRANSACTIONTYPE"]);
                //Response.Write("<br />PAYMENTTYPE: " + decoder["PAYMENTTYPE"]);
                //Response.Write("<br />PAYMENTSTATUS: " + decoder["PAYMENTSTATUS"]);
                //Response.Write("<br />FEEAMT: " + decoder["FEEAMT"]);
                //Response.Write("<br />ORDERTIME: " + decoder["ORDERTIME"]);
                NVPAPICaller NENANVP = new NVPAPICaller();

                string retMsg = "";
                string strtoken = "";
                string finalPaymentAmount = "";
                string payerId = "";
                int intAffiliateID = Convert.ToInt32(Session["Affiliate_ID"].ToString());
                //NVPCodec decoder;
                var decoder = new NVPCodec();


                strtoken = Request.QueryString["token"] == "" ? Session["token"].ToString() : Request.QueryString["token"];
                payerId = Request.QueryString["PayerId"] == "" ? Session["PayerId"].ToString() : Request.QueryString["PayerId"];

                finalPaymentAmount = Session["payment_amt"].ToString();
                Response.Write("payment_amt: " + finalPaymentAmount);
                Response.Write("<br />payerId: " + payerId);
                Response.Write("<br />token: " + strtoken);

                bool boolSuccess = NENANVP.ConfirmPayment(finalPaymentAmount, "NENA Event Fees", strtoken, payerId, ref decoder, ref retMsg, intAffiliateID);

                Response.Write("<br />Amount: " + decoder["PAYMENTINFO_0_AMT"]);
                Response.Write("<br />Currency: " + decoder["PAYMENTINFO_0_CURRENCYCODE"]);
                Response.Write("<br />transactionId: " + decoder["PAYMENTINFO_0_TRANSACTIONID"]);
                Response.Write("<br />TRANSACTIONTYPE: " + decoder["PAYMENTINFO_0_TRANSACTIONTYPE"]);
                Response.Write("<br />PAYMENTTYPE: " + decoder["PAYMENTINFO_0_PAYMENTTYPE"]);
                Response.Write("<br />PAYMENTSTATUS: " + decoder["PAYMENTINFO_0_PAYMENTSTATUS"]);
                Response.Write("<br />FEEAMT: " + decoder["PAYMENTINFO_0_FEEAMT"]);
                Response.Write("<br />ORDERTIME: " + decoder["PAYMENTINFO_0_ORDERTIME"]);

                //ErrHandler.WriteError("BoolSuccess: " + boolSuccess + "\r\nFor TokenID: " + strtoken);
                if (boolSuccess)
                {
                    Response.Write("boolSuccess");
                    //Update the PaymentTransaction based on tokenID
                    PayPalTransactions transCheckout = new PayPalTransactions();
                    //1) mark the transaction paid
                    int intMemberID = Convert.ToInt32(Session["Member_ID"]);
                    int intTransactionID = Convert.ToInt32(Session["TransactionPaymentID"]);
                    int intEventId = Convert.ToInt32(Session["EventID"]);
                    
                    transCheckout.UpdateFullPayPal_byTranactionPaymentId(strtoken, payerId, decoder, intMemberID, intTransactionID);

                    //send the confirmation email
                    Message.EventPaymentCompleted(intMemberID, intTransactionID, intEventId);
                    
                    //redirects to the reciept
                    //Response.Redirect("/paypal/ViewTransactionReciept.aspx?Member_ID=" + intMemberID + "&TransactionID=" + intTransactionID, false);
                    //redirects to the session summary
                    //Response.Redirect("http://events.nena.ca/Confirmation/Conference/NENA2011.aspx?MemberId=" + intMemberID + "&EventId=" + intEventId, true);
                    Response.Redirect("http://events.nena.ca/Confirmation/Conference/NENAConference2012.aspx?MemberId=" + intMemberID + "&EventId=" + intEventId, true);
                }
                else
                {
                    Response.Write("boolSuccess= " + boolSuccess);
                    //ErrHandler.WriteError("Transaction failed for member: " + intMemberID + "\r\n TransactionID: " + intTransactionID);
                    Response.Redirect("APIError.aspx?" + retMsg, false);
                }
            }
            catch (Exception ex)
            {
                ErrHandler.WriteError("In OrderConfirmation: OrderConfirmation: <br/>" + ex.Message);
                Response.Write("Sorry, an Error Occurred During this Transaction <br /> Please try again in a few minutes as the PayPal system may not be available<br />");
                Response.Write(ex.InnerException + "<br />" + ex.Message + "<br />");
            }
        }
    }
}